<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入关键字"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否标记" prop="isLike">
          <el-select 
          v-model="queryParams.isLike" 
          placeholder="请选择是否标记" 
          class="diy-w-full" clearable>
            <el-option
              v-for="(item,index) in isBiaoji"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <TableCard>
        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

          <el-table :border="Global.tableShowBorder" v-loading="loading" :data="list">
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="公司名称" align="center" prop="company"/>
            <el-table-column label="联系手机号" align="center" prop="tel"/>
            <el-table-column label="联系邮箱" align="center" prop="email"/>
            <el-table-column label="是否标记" align="center" prop="likedStr"/>
            <el-table-column label="创建时间" align="center" prop="createTime"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="{row}">
                <el-button
                type="text"
                @click="handleDetail(row,1)"
                v-hasPermi="['cooperate:dayReport:query1']">查看详情</el-button>
                <el-dropdown size="mini" @command="(command) => handleCommand(command, row)" v-if="getCaozuo(row).length">
                  <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item,index) in getCaozuo(row)" :key="index" :command="item.command" :icon="item.icon"
                    >{{item.label}}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
      </TableCard>
    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row v-if="formType === 2">
          <el-col :span="24">
            <el-col :span="24">
              <el-form-item label="描述内容" prop="content">
                <el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入描述内容"/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
        <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-else>
          <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
            <div class="flex_hcneter">
              <svg-icon v-if="item.prop != 'attachmentList'" icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.prop == 'content' && item.isCustom">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="formType === 2" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="fkTitle" :visible.sync="fkOpen" width="90%" append-to-body>
      <TableCard>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd(2)"
              v-hasPermi="['cooperate:customerIntentFeedback:add']"
            >新增
            </el-button>
          </el-col>
          <right-toolbar @queryTable="listCustomerIntentFeedback" :searchBtn="false"></right-toolbar>
        </el-row>

          <el-table :border="Global.tableShowBorder" v-loading="fkLoading" :data="fkList">
            <el-table-column label="创建者用户名" align="center" prop="userName"/>
            <el-table-column label="创建者昵称" align="center" prop="nickName"/>
            <el-table-column label="用户头像" align="center" prop="avatar">
              <template slot-scope="{row}">
                <el-image v-if="row.avatar"
                  style="width: 100px; height: 100px"
                  :src="row.avatar"
                  :preview-src-list="[row.avatar]">
                </el-image>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
              <template slot-scope="{row}">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(row,2)"
                  v-hasPermi="['cooperate:customerIntentFeedback:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(row,2)"
                  v-hasPermi="['cooperate:customerIntentFeedback:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="fkTotal>0"
            :total="fkTotal"
            :page.sync="fkQueryParams.pageNum"
            :limit.sync="fkQueryParams.pageSize"
            @pagination="listCustomerIntentFeedback"
          />
      </TableCard>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fkOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCustomerIntent,getCustomerIntent,
  addCustomerIntentLike,delCustomerIntentLike,
  listCustomerIntentFeedback,getCustomerIntentFeedback,addCustomerIntentFeedback,updateCustomerIntentFeedback,delCustomerIntentFeedback,} from "@/api/collaWorryFree/webCustorInfor";

export default {
  name: "WebCustorInfor",
  dicts: [],
  data() {
    return {
      formDetail:[
        {label:'名称',prop:'name',},{label:'公司名称',prop:'company',},{label:'联系手机号',prop:'tel',},{label:'联系邮箱',prop:'email',span:24,},
        {label:'汇报内容',prop:'content',isCustom:true,span:24,},{label:'联系地址',prop:'address',span:24,},{label:'是否标记',prop:'likedStr',},
        {label:'创建时间',prop:'createTime',},
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [ {required: true, message: "反馈内容不能为空", trigger: "blur"} ],
      },
      // 弹出层标题
      title: "",
      fkTitle: "",
      // 是否显示弹出层
      open: false,
      fkOpen: false,
      // 遮罩层
      loading: true,
      fkLoading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fkTotal: 0,
      // 表格数据
      list: [],
      fkList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keywords:null,isLike:null,
      },
      fkQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      isBiaoji:[
        {label:'是',value:true,},
        {label:'否',value:false,},
      ],
      bjPermission:'cooperate:customerIntentLike:add',//标记
      qxbjPermission:'cooperate:customerIntentLike:remove',//取消标记
      fkPermission:'cooperate:customerIntentFeedback:list',//录入客户信息反馈列表
      formType:1,//表单显示类型
    };
  },
  created() {
      this.getList();
  },
  methods: {
    handleFankui(row){//反馈列表
      this.fkOpen = true
      this.fkTitle = '反馈列表'
      this.fkQueryParams.customerId = row.id
      this.fkQueryParams.pageNum = 1;
      this.listCustomerIntentFeedback()
    },
    async listCustomerIntentFeedback(){//查询录入客户信息反馈列表
      this.fkLoading = true
      let res = await listCustomerIntentFeedback(this.fkQueryParams)
      let rows = res.rows || []
      this.fkList = rows
      this.fkTotal = res.total || 0
      this.fkLoading = false
    },
    getCaozuo(row){//获取操作按钮
      const user = this.$store.state.user
      const permissions = user.permissions
      const flag = !!permissions.find(item => item == this.bjPermission)
      const flag1 = !!permissions.find(item => item == this.qxbjPermission)
      const flag2 = !!permissions.find(item => item == this.fkPermission)
      let caozuoList = []
      if(!row.liked  && (flag || permissions[0] == '*:*:*') ) {
        caozuoList.push({label:'标记',command:'handleBiaoji',icon:'',})
      }
      if( row.liked && (flag1 || permissions[0] == '*:*:*')) {
        caozuoList.push({label:'取消标记',command:'handleBiaoji',icon:'',})
      }
      if( flag2 || permissions[0] == '*:*:*' ) {
        caozuoList.push({label:'反馈列表',command:'handleFankui',icon:'',})
      }
      return caozuoList
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleBiaoji":
          this.handleBiaoji(row);
          break;
        case "handleFankui":
          this.handleFankui(row);
          break;
      }
    },
    handleDetail(row,type) {//查看详情
      this.formType = type
      const id = row.id
      this.open = true;
      this.title = "查看详情";
      this.getDetail(id)
    },
    async getDetail(id){
      let response = await getCustomerIntent(id)
      let data = response.data || {}
      this.form = {
        ...data,
        content:this.$changeLine(data.content || ''),
        likedStr:data.liked ? '是' : '否'
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      listCustomerIntent(queryParams).then(response => {
        let rows = response.rows || [];
        this.list = rows.map(item=>{
          return {
            ...item,likedStr:item.liked ? '是' : '否'
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(type) {
      this.formType = type
      this.reset();
      this.form.customerId = this.fkQueryParams.customerId
      this.open = true;
      this.title = "添加意向客户信息反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row,type) {
      this.formType = type
      this.reset();
      const id = row.id || this.ids
      getCustomerIntentFeedback(id).then(response => {
        let data = response.data || {};
        this.form = { ...data, }
        this.open = true;
        this.title = "修改意向客户信息反馈";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          if (this.form.id != undefined) {
            updateCustomerIntentFeedback(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.listCustomerIntentFeedback()
            });
          } else {
            addCustomerIntentFeedback(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.listCustomerIntentFeedback()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row,type) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
          return delCustomerIntentFeedback(id);
      }).then(() => {
        this.listCustomerIntentFeedback()
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 标记按钮操作 */
    handleBiaoji(row) {
      const id = row.id
      let api = row.liked ? delCustomerIntentLike : addCustomerIntentLike
      this.$modal.confirm(`是否确认${row.liked ? '取消' : ''}标记该数据项？`).then(function () {
          return api(id);
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
  }
};
</script>
